function e(e) {
  return e && e.__esModule ? e : {
    default: e
  };
}

var t = Object.assign || function(e) {
    for (var t = 1; t < arguments.length; t++) {
      var a = arguments[t];
      for (var i in a) Object.prototype.hasOwnProperty.call(a, i) && (e[i] = a[i]);
    }
    return e;
  },
  a = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function(e) {
    return typeof e;
  } : function(e) {
    return e && "function" == typeof Symbol && e.constructor === Symbol && e !== Symbol.prototype ? "symbol" : typeof e;
  },
  i = e(require("../../../api/api.js")),
  o = e(require("../../../utils/util.js")),
  r = e(require("../../../utils/filter.js")),
  s = require("../../../config/constants.js"),
  n = require("../../../data/translate.js"),
  l = require("../../../data/translate_tag.js"),
  config = require('../../../config'),
  c = getApp();

Page({
  data: {
    cater_detail: " ",
    cater_lable:"",
    personal_label:"",
    inputValue: "",
    teacher_id: "",
    teacher_profile_data: {},
    sessionCourses: [],
    showMoreIntro: !1,
    hasMore: !1,
    latestSessionPage: 1,
    latestSessionPageSize: 5,
    availableTimeDatum: [],
    latest_session_list: [],
    show_index: [!1, !1, !1, !1],
    toView: "",
    moveItemNumber: "moveItem-1",
    isMove: !1
  },
  //zl：通过点击当前达人进入
  onLoad: function (e) {
    console.log(e.id);
    var that = this
    wx.request({
      url: config.service.detailUrl,
      data: {
        cater_id: e.id,
        },
      header: {
        'content-type': 'application/json' // 默认值
      },
      success: function (res) {
        //这里返回的是数组对象，只取第一组
        var reg = new RegExp("\"", "g");
        a = res.data[0].cater_label.slice(1, -1).replace(RegExp("\"", "g"),"").split(",");
      console.log(a);
        that.setData({
          cater_label: a
        })
        var b = res.data[0].personal_label.slice(1, -1).replace(reg, "").split(",");
        console.log(b);
        that.setData({
          personal_label: b
        })
        that.setData({
          cater_detail: res.data[0]
        })
      },
      fail: function (res) {
        console.log(res.data)
      }
    })
    //这里采用原有版本，模拟一个时间
    this.getTeacherAvailableTime(4549902)
  },

  //zl：选择时间和服务内容
  bindToCourse: function (e) {
    //当前课程
    console.log(this.data.cater_detail.cater_id)
    var res = e.currentTarget.id
    console.log(res)
    var a = "/pages/courses/courses?id=" + this.data.cater_detail.cater_id + "&title=" + res;
    wx.navigateTo({
      url: a
    });
  },

  bindViewTap: function() {
    wx.navigateTo({
      url: "../../logs/logs"
    });
  },
  redirectToTeacherPage: function() {
    wx.switchTab({
      url: "/pages/teacher/teacher"
    });
  },

  fetchTeacherProfileById: function(e) {
    var t = this;
    i.default.getTeacherProfileById({
      query: {
        id: e
      },
      success: function(e) {
        if (e && e.data.data) {
          var a = e.data.data;
          t.mergeTeacherInfo(a), t.setData({
            teacher_profile_data: a
          }), t.fetchCourseListByTeacherId(a.id), t.getTeacherAvailableTime(a.id), t.fetchLatestSessions(a.id, a.textid);
        }
      }
    });
  },
  mergeTeacherInfo: function(e) {
    if (e.teacher_avatar_file = s.CDN_ADDR + e.avatar_file_name + "_Avatar.jpg", e.teacher_country_flag = "/images/flags/" + e.origin_country_id.toLowerCase() + "3x.png",
      e.teacher_living_country_flag = "/images/flags/" + e.living_country_id.toLowerCase() + "3x.png",
      e.teacher_country_text = r.default.convertSymbolToText(e.origin_country_id, n.COUNTRIES_DATUM) || e.origin_country_id,
      e.teacger_living_country = r.default.convertSymbolToText(e.living_country_id, n.COUNTRIES_DATUM) || e.living_country_id,
      e.teacher_now_time = e.teacher_local_time.slice(11, 16) + " " + e.teacher_timezone_utc,
      e.teacher_experience_obj_s && e.teacher_experience_obj_s.map(function(e) {
        e.countryname = o.default.countryName(e.country);
      }), e.teacher_start_time = o.default.formatCurrentDate(e.teacher_info_obj.first_valid_time),
      e.language_obj_s.map(function(e) {
        e.lan_text = r.default.convertSymbolToText(e.language, n.LANGUAGE_DATUM) || e.language;
      }), e.teacher_info_obj.min_price_cny = r.default.convertCurrencyToCNY(e.teacher_info_obj.min_price_usd, c.globalData.currencyDatum),
      e.teacher_info_obj.trial_price_cny = r.default.convertCurrencyToCNY(e.teacher_info_obj.trial_price_usd, c.globalData.currencyDatum),
      o.default.orderBy(e.language_obj_s, ["level"], "desc"), e.teacher_info_obj.teacher_tags) {
      var t = e.teacher_info_obj.teacher_tags.split(",");
      t = t.map(function(e) {
        return r.default.convertSymbolToText(e, l.TAG_DATUM);
      }), e.teacher_info_obj.teacher_tags_text = t;
    }
    if (e.teacher_info_obj.teacher_child_tags) {
      var a = e.teacher_info_obj.teacher_child_tags.split(",");
      a = a.map(function(e) {
        return r.default.convertSymbolToText(e, l.CHILD_TAG_DATUM);
      }), e.teacher_info_obj.teacher_child_tags_text = a;
    }
    e.is_schedule && !e.has_had_trial && e.trial_left_count || 0 === Object.keys(c.globalData.userPersonalInfo).length ? e.hide_trial_course = !0 : e.hide_trial_course = !1;
  },
  fetchTeacherPersonalTagById: function(e) {
    var t = this,
      a = {
        teacher_id: e
      };
    i.default.getTeacherPersonalTagById({
      data: a,
      success: function(e) {
        if (e && e.data.data) {
          var a = e.data.data.tag_list.slice(0, 5);
          t.setData({
            teacher_personal_tag: a
          });
        }
      }
    });
  },
  fetchCourseListByTeacherId: function(e) {
    var t = this;
    i.default.getCourseListByTeacherId({
      query: {
        id: e
      },
      success: function(e) {
        if (e && e.data.data) {
          var i = e.data.data;
          i.map(function(e) {
            e.language_text = r.default.convertSymbolToText(e.language, n.LANGUAGE_DATUM) || e.language,
              e.session_price_cny = r.default.convertCurrencyToCNY(e.session_price_usd, c.globalData.currencyDatum),
              e.trial_price_cny = r.default.convertCurrencyToCNY(e.trial_price_usd, c.globalData.currencyDatum);
          });
          var o = [],
            s = [];
          (i || []).forEach(function(e, t) {
            "object" === (void 0 === e ? "undefined" : a(e)) && (e.title ? o.push(e) : s.push(e));
          }), t.data.teacher_profile_data.proCourseList = o, t.data.teacher_profile_data.tutorCourseList = s;
          var l = t.data.teacher_profile_data;
          t.setData({
            teacher_profile_data: l
          });
        }
      }
    });
  },

  getTeacherAvailableTime: function(e) {
    var t = this,
     
      a = wx.getStorageSync("token") || "",
      r = {
        start_time: o.default.getDateStr(1) + " 00:00",
        end_time: o.default.getDateStr(7) + " 23:59",
        i_token: a
      };
    i.default.getTeacherAvailableTime({
      query: {
        id: e
      },
      data: r,
      success: function(e) {
        if (e && e.data.data) {
          //zl：注意这里a和e表示的时间格式：
          console.log(e);
          console.log(e.data.data);
          var a = e.data.data.slice(0, 7);
          // console.log(a);
          //每天都分成48个半小时，按照大小定权重，进而决定深浅。
          //这里直接指定，就不需要调用后面的handlerAvailableTime函数了。
          a.forEach(t.handlerAvailableTime),
            t.setData({
              availableTimeDatum: a
            });
        }
      }
    });
  },
  //zl: 这里的t和a，是调用次函数的对象传过来的，注意这种用法。
  handlerAvailableTime: function(e) {
    e.week_day = o.default.getWeekByDate(e.day),
      e.half_day = [{
        index: 0,
        availableFlag: !1,
        availableWeight: 0
      }, {
        index: 1,
        availableFlag: !1,
        availableWeight: 0
      }, {
        index: 2,
        availableFlag: !1,
        availableWeight: 0
      }, {
        index: 3,
        availableFlag: !1,
        availableWeight: 0
      }],
      e.hours.map(function(t, a) {
        a >= 0 && a <= 12 && !0 === t.is_show && (e.half_day[3].availableFlag = !0, e.half_day[3].availableWeight++),
          a > 12 && a <= 24 && !0 === t.is_show && (e.half_day[0].availableFlag = !0, e.half_day[0].availableWeight++),
          a > 24 && a <= 36 && !0 === t.is_show && (e.half_day[1].availableFlag = !0, e.half_day[1].availableWeight++),
          a > 36 && a <= 48 && !0 === t.is_show && (e.half_day[2].availableFlag = !0, e.half_day[2].availableWeight++);
      });
    console.log(e);
  },

  fetchLatestSessions: function(e, t) {
    var a = this,
      l = {
        textid: t,
        page: a.data.latestSessionPage,
        page_size: a.data.latestSessionPageSize
      };
    i.default.getLatestSessions({
      query: {
        id: e
      },
      data: l,
      success: function(e) {
        var t = a.data.latest_session_list;
        e && e.data && 1 === l.page && (t = []);
        var i = e.data.data;
        i.length > 0 && (i.map(function(e) {
          e.avatar_file_name_url = s.CDN_ADDR + e.oppo_user_obj.avatar_file_name + "_Avatar.jpg",
            e.session_start_time_user = o.default.formatCurrentDate(e.session_start_time_user),
            e.learn_language_text = r.default.convertSymbolToText(e.learn_language, n.LANGUAGE_DATUM) || e.learn_language,
            e.moreComments = [];
        }), i.length < a.data.latestSessionPageSize ? a.setData({
          latest_session_list: t.concat(i),
          hasMore: !1
        }) : a.setData({
          latest_session_list: t.concat(i),
          hasMore: !0,
          latestSessionPage: a.data.latestSessionPage + 1
        }));
      }
    });
  },
  showMoreComment: function() {
    this.fetchLatestSessions(this.data.teacher_profile_data.id, this.data.teacher_profile_data.textid);
  },
  showMoreSessionComments: function(e) {
    for (var a = this, s = void 0, l = this.data.latest_session_list.slice(), c = 0; c < l.length; c++)
      if (l[c].student_textid === e.currentTarget.dataset.studentId) {
        s = l[c];
        break;
      }
    s.page ? s.page = s.page + 1 : (s.page = 1, s.moreComments = []);
    var h = {
      textid: e.currentTarget.dataset.teacherId,
      student_textid: e.currentTarget.dataset.studentId,
      page: s.page,
      page_size: 5
    };
    i.default.getTeacherReviews({
      query: {
        id: this.data.teacher_id
      },
      data: h,
      success: function(e) {
        e.data.data.forEach(function(e) {
          e.session_start_time_user = o.default.formatCurrentDate(e.session_start_time_user),
            e.learn_language_text = r.default.convertSymbolToText(e.learn_language, n.LANGUAGE_DATUM),
            s.moreComments.push(e);
        }), a.setData(t({}, a.data, {
          latest_session_list: l
        }));
      }
    });
  },
  toggleTeacherIntroShow: function(e) {
    this.data.showMoreIntro = !this.data.showMoreIntro;
    var t = this.data.showMoreIntro;
    this.setData({
      showMoreIntro: t
    });
  },
  toggleShow: function(e) {
    var t = e.currentTarget.dataset.index;
    this.data.show_index[t] = !this.data.show_index[t];
    var a = this.data.show_index;
    this.setData({
      show_index: a
    });
  },

  modalBindToCourse: function(e) {
    if (0 === Object.keys(c.globalData.userPersonalInfo).length) wx.switchTab({
      url: "/pages/auth/auth"
    });
    else {
      var t = e.currentTarget.dataset.item;
      if (t.nickname) a = "/pages/courses/courses?id=" + this.data.teacher_id;
      else var a = "/pages/courses/courses?id=" + this.data.teacher_id + "&course_id=" + t.id;
      wx.navigateTo({
        url: a
      }), this.easyModal.hide();
    }
  },
  onImageError: function(e) {
    var t = this;
    o.default.errImgFun(e, t);
  },
  onShowCourseModal: function(e) {
    this.easyModal = this.selectComponent("#easyModal");
    var t = e.currentTarget.dataset.name;
    this.setData({
      courseInfo: t
    }), this.easyModal.show();
  },
  onReady: function() {
    this.easyModal = this.selectComponent("#easyModal"), wx.setNavigationBarTitle({
      title: "达人资料库"
    });
  },
  choicePanel: function(e) {
    var t = e.currentTarget.dataset.panelindex;
    this.setData({
      toView: t
    });
    var a = this;
    wx.createSelectorQuery().select(".move-item").boundingClientRect(function(e) {
      "profilepanel" == t && a.setData({
        moveItemNumber: "moveItem-1"
      }), "coursepanel" == t && a.setData({
        moveItemNumber: "moveItem-2"
      }), "availabletimepanel" == t && a.setData({
        moveItemNumber: "moveItem-3"
      }), "commentpanel" == t && a.setData({
        moveItemNumber: "moveItem-4"
      });
    }).exec();
  },
  moveItem: function() {
    var e = this;
    wx.createSelectorQuery().selectAll(".nav-panel").boundingClientRect(function(t) {
      var a = t[0].height,
        i = t[0].height + t[1].height,
        o = t[0].height + t[1].height + t[2].height,
        r = t[0].height + t[1].height + t[2].height + t[3].height;
      e.data.scrollTop + 1 >= r ? (e.setData({
        moveItemNumber: "moveItem-4"
      }), console.log(r)) : e.data.scrollTop + 1 >= o ? e.setData({
        moveItemNumber: "moveItem-3"
      }) : e.data.scrollTop + 1 >= i ? e.setData({
        moveItemNumber: "moveItem-2"
      }) : e.data.scrollTop + 1 >= a && e.setData({
        moveItemNumber: "moveItem-1"
      });
    }).exec();
  },
  throttle: function(e) {
    this.setData({
      scrollTop: e.detail.scrollTop
    });
    var t = this,
      a = wx.createSelectorQuery();
    a.select("#above-nav-height").boundingClientRect(), a.exec(function(e) {
      t.data.scrollTop >= e[0].height ? t.setData({
        navToTop: !0
      }) : t.setData({
        navToTop: !1
      });
    });
    var i = function(e) {
      wx.createSelectorQuery().selectAll(".nav-panel").boundingClientRect(function(t) {
        var a = t[0].height,
          i = t[0].height + t[1].height,
          o = t[0].height + t[1].height + t[2].height,
          r = t[0].height + t[1].height + t[2].height + t[3].height;
        e.data.scrollTop + 1 >= r ? (e.setData({
          moveItemNumber: "moveItem-4"
        }), console.log(r)) : e.data.scrollTop + 1 >= o ? e.setData({
          moveItemNumber: "moveItem-3"
        }) : e.data.scrollTop + 1 >= i ? e.setData({
          moveItemNumber: "moveItem-2"
        }) : e.data.scrollTop + 1 >= a && e.setData({
          moveItemNumber: "moveItem-1"
        });
      }).exec();
    };
    t.data.isMove || (t.setData({
      isMove: !0
    }), setTimeout(function() {
      i(t), t.setData({
        isMove: !1
      });
    }, 200));
  },
  scroll: function(e) {
    this.setData({
      scrollTop: e.detail.scrollTop
    });
    var t = this,
      a = wx.createSelectorQuery();
    a.select("#above-nav-height").boundingClientRect(), a.exec(function(e) {
      t.data.scrollTop >= e[0].height ? t.setData({
        navToTop: !0
      }) : t.setData({
        navToTop: !1
      });
    }), o.default.throttle(this.moveItem, 100, {
      leading: !1
    })();
  }
});